package com.xiyue.leaspring.util.dbc;

import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

public class RedisConnectionUtil3 {

	private static final String MASTER_NAME = "mymaster";
	private static final int MAX_TOTAL = 200;
	private static final int MAX_IDLE = 20;
	private static final int MAX_WAIT_MILLIS = 1000;
	private static final boolean TEST_ON_BORROW = true;
	private JedisSentinelPool jedisPool;
	
	public RedisConnectionUtil3() {
		Set<String> sentinels = new HashSet<String>();
		sentinels.add("redis-master:26379");
		sentinels.add("redis-slave-a:26380");
		sentinels.add("redis-slave-b:26381");
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(MAX_TOTAL);
		config.setMaxIdle(MAX_IDLE);
		config.setMaxWaitMillis(MAX_WAIT_MILLIS);
		config.setTestOnBorrow(TEST_ON_BORROW);
		this.jedisPool = new JedisSentinelPool(MASTER_NAME,sentinels,config);
	}
	
	public void close() {
		this.jedisPool.close();
	}
	
	public Jedis getConnection() {
		return this.jedisPool.getResource();
	}
	
}
